package com.free.framework.lib.image;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.DrawableRes;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
//import com.bumptech.glide.MemoryCategory;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.bumptech.glide.request.target.Target;
//import com.free.framework.lib.utils.ContextHolder;
//import com.jakewharton.picasso.OkHttp3Downloader;
//import com.squareup.picasso.MemoryPolicy;
//import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.concurrent.ExecutionException;

//import okhttp3.Cache;
//import okhttp3.OkHttpClient;

/**
 * @author free
 * @date 16/10/24
 */

public class ImageLoader {

//    private static final int CACHE_SIZE = 10 * 1024 * 1024;
//
//    private static final OkHttpClient.Builder builder = new OkHttpClient().newBuilder()
//            .cache(new Cache(ContextHolder.getContext().getCacheDir(), CACHE_SIZE));

//    static {
//        Picasso.setSingletonInstance(new Picasso.Builder(ContextHolder.getContext())
//                .downloader(new OkHttp3Downloader(builder.build())).build());
//    }

    public static void load(String imageUrl, ImageView view) {
//        Picasso.with(ContextHolder.getContext()).load(imageUrl).into(view);
        Glide.with(view.getContext()).load(imageUrl).fitCenter().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(view);
    }

    public static void loadCircle(String imageUrl, ImageView view){
        Glide.with(view.getContext()).load(imageUrl).bitmapTransform(new GlideCircleTransform(view.getContext())).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(view);
    }

    public static void loadFileNoCache(File file, ImageView view) {
//        Picasso.with(ContextHolder.getContext()).load(file).memoryPolicy(MemoryPolicy.NO_CACHE).into(view);
        Glide.with(view.getContext()).load(file).fitCenter().diskCacheStrategy(DiskCacheStrategy.NONE).into(view);
    }

    public static void loadWithDefaultImage(String imgUrl,@DrawableRes int defaultImgId, ImageView imageView){
//        Picasso.with(ContextHolder.getContext()).load(imgUrl).placeholder(defaultImgId).into(imageView);
        Glide.with(imageView.getContext()).load(imgUrl).fitCenter().diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(defaultImgId).into(imageView);
    }

    public static Bitmap getBitmap(String imageUrl, Context context) {
        try {
            return Glide.with(context)
                    .load(imageUrl)
                    .asBitmap()
                    .into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                    .get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }


}